package com.zwStorage.Controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zwStorage.Model.AAA;
import com.zwStorage.Model.AAAA;
import com.zwStorage.Model.other;
import com.zwStorage.Util.Util;
import org.jboss.logging.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * zengwei
 * 日期：2020/5/27 23:40
 */
@Controller
public class DataController {

    @ResponseBody
    @GetMapping("/getOther")  /**获取其他数据**/
    public other getOther(){
        return Util.getOhter();
    }


    @ResponseBody
    @PostMapping("/AddAAA")  /**添加入库数据**/
    public boolean AddAAA(@RequestBody String json){
        Gson gson = new Gson();
        AAA aaa = gson.fromJson(json, AAA.class);
        aaa.setTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        aaa.setOutBound(false);

        return Util.addAAA(aaa);
    }


    @ResponseBody
    @GetMapping("/getAllAAA")  /**获取所有数据**/
    public AAAA getAllAAA(int page){
        List<AAA> aaas=Util.getAAA();
        return new AAAA(aaas.size(),pageBySubList(aaas,10,page));
    }
    public static List<AAA> pageBySubList(List list, int pagesize, int currentPage) {
        int totalcount = list.size();
        int pagecount = 0;
        List<AAA> subList;
        int m = totalcount % pagesize;
        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {
            pagecount = totalcount / pagesize;
        }
        if (m == 0) {
            subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = list.subList((currentPage - 1) * pagesize, totalcount);
            } else {
                subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
            }
        }
        return subList;
    }




    @ResponseBody
    @GetMapping("/getWOPrice")  /**出库金额和入库金额**/
    public String getWOPrice(){
        double w=0;
        double o=0;
        for (AAA a:Util.getAAA()){
            if (a.isOutBound()){
                o+=a.getPrice();
            }else {
                w+=a.getPrice();
            }
        }
        return "{\"o\":"+o+",\"w\":"+w+"}";
    }

    @ResponseBody
    @PostMapping("/setoutbound")  /**出库金额和入库金额**/
    public boolean setoutbound(@RequestBody String json){
        System.out.println(json);
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        Util.outbound(jsonObject.get("id").getAsInt());
        return true;
    }


//    @ResponseBody
//    @GetMapping("/getAllAAA")  /**筛选数据**/
//    public AAAA getAllAAA(String name,String Brand,String Type,String Specifications){
//        List<AAA> aaas=Util.getAAA();
//        List<AAA> muAAA=new ArrayList<>();
//        for(){
//
//        }
//        return new AAAA(aaas.size(),pageBySubList(aaas,10,page));
//    }

}
